/**
 * Department数据传输对象
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.dto;


import com.sxf.crm.entity.Department;
import lombok.Data;

/**
 * 部门数据传输对象
 */
@Data
public class DepartmentDTO {
    private Long id;
    private String name;
    private String description;
    private String fullPath;
    private Long parentId;
    private String parentName;
    private Long createUserId;
    private String creatorName;
    private Long createAt;
    private Long updateUserId;
    private String updaterName;
    private Long updateAt;

    public static DepartmentDTO fromEntity(Department dept) {
        if (dept == null) {
            return null;
        }

        DepartmentDTO dto = new DepartmentDTO();
        dto.setId(dept.getId());
        dto.setName(dept.getName());
        dto.setDescription(dept.getDescription());
        dto.setFullPath(getFullPathName(dept));

        if (dept.getParent() != null) {
            dto.setParentId(dept.getParent().getId());
            dto.setParentName(dept.getParent().getName());
        }

        dto.setCreateUserId(dept.getCreateUserId());
        dto.setCreateAt(dept.getCreateAt());
        dto.setUpdateUserId(dept.getUpdateUserId());
        dto.setUpdateAt(dept.getUpdateAt());

        return dto;
    }

    private static String getFullPathName(Department department) {
        if (department.getParent() == null) {
            return department.getName();
        }
        return getFullPathName(department.getParent()) + " / " + department.getName();
    }
}
